package com.ziyougou.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ziyougou.product.vo.AttrGroupRelationVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyougou.common.utils.PageUtils;
import com.ziyougou.common.utils.Query;

import com.ziyougou.product.dao.AttrAttrgroupRelationDao;
import com.ziyougou.product.entity.AttrAttrgroupRelationEntity;
import com.ziyougou.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public void deleteByAttrIdAndGroupId(List<AttrGroupRelationVO> attrGroupRelationVOList) {

        baseMapper.deleteByAttrIdAndGroupId(attrGroupRelationVOList);
    }

    @Override
    public List<Long> selectAttrIdsByGroupId(Integer attrgroupId) {
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrAttrgroupRelationEntity::getAttrGroupId,attrgroupId);

        List<AttrAttrgroupRelationEntity> relationEntityList = baseMapper.selectList(wrapper);
        //筛选
        List<Long> ids = relationEntityList.stream().map(relationEntity -> {
            return relationEntity.getAttrId();
        }).collect(Collectors.toList());
        return ids;
    }

    @Override
    public AttrAttrgroupRelationEntity selectAttrGroupIdByAttrId(Long attrId) {
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrAttrgroupRelationEntity::getAttrId,attrId);
        AttrAttrgroupRelationEntity relationEntity = baseMapper.selectOne(wrapper);
        return relationEntity;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

}